<?php
class Model_DbTable_Product extends Apps_Db_Table_Abstract {
	protected $_name = 'product';

	const STATUS_ACTIVE   = 1;
	const STATUS_INACTIVE = 0;
	const STATUS_DELETED  = -1;

	const SHOW_HOME       = 1;
	const NOT_SHOW_HOME   = 0;


	public function listOther($productId, $categoryId) {
		$q = $this->_db
				  ->select()
				  ->from($this->_name, null)
				  ->where('status =?', self::STATUS_ACTIVE)
				  ->where('product_category_id =?', $categoryId)
				  ->where('product.id !=?', $productId)
				  ->columns(array(
		 				   'id' => 'product.id',
        	               'name' => 'product.name',
        	               'code' => 'product.code',
        	               'price_sale' => 'product.price_sale',
        	               'thumbnail_link' => 'product.thumbnail_link'));

 		return $q->query()->fetchAll();
	}


	public function getSelectGrid() {
	    $select = $this->_db
                	    ->select()
                	    ->from('product', null)
                	    ->join(array('c1' => 'product_category'), 'c1.id = product.product_category_id', null)
                	    ->joinLeft(array('c2' => 'product_category'), 'c2.id = c1.parent_id', null)
                	    ->join('product_status', 'product_status.id = product.product_status_id', null)
                	    ->where('product.status =?', self::STATUS_ACTIVE)
                	    ->where('c1.status =?', Model_DbTable_ProductCategory::STATUS_ACTIVE)
                	    ->columns(array(
                	            'id'                     => 'product.id',
                	            'code'                   => 'product.code',
                	            'name'                   => 'product.name',
                	            'price_original'         => 'product.price_original',
                	            'price_sale'             => 'product.price_sale',
                	            'price_sale_off'         => 'product.price_sale_off',
                	            'quantity'               => 'product.quantity',
                	            'size'                   => 'product.size',
                	            'color'                  => 'product.color',
                	            'thumbnail_link'         => 'product.thumbnail_link',
                	            'status'                 => 'product.status',
                	            'status_label'           => new Zend_Db_Expr('CASE product.status WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                	            'is_new'                 => 'product.is_new',
                	            'is_new_label'           => new Zend_Db_Expr('CASE product.is_new WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                	            'is_sale_off'            => 'product.is_sale_off',
                	            'is_sale_off_label'      => new Zend_Db_Expr('CASE product.is_sale_off WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                	            'is_featured'            => 'product.is_featured',
                	            'is_featured_label'      => new Zend_Db_Expr('CASE product.is_featured WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                	            'is_best_selling'        => 'product.is_best_selling',
                	            'is_best_selling_label'  => new Zend_Db_Expr('CASE product.is_best_selling WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
                	            'product_category_title' => 'c1.title',
                	            'parent_product_category_title' => 'c2.title',
                	            'product_status_title'   => 'product_status.title',
                	    ));
	   return $select;
	}

	public function listByCategoryId($categoryId) {
        $select = $this->_db
        	       ->select()
        	       ->from('product', null)
        	       ->where('status =? ', self::STATUS_ACTIVE)
        	       ->where('product_category_id =? ', $categoryId)
        	       ->columns(array(
        	               'id' => 'product.id',
        	               'name' => 'product.name',
        	               'code' => 'product.code',
        	               'price_sale' => 'product.price_sale',
        	               'thumbnail_link' => 'product.thumbnail_link'
        	       ));

        return $select->query()->fetchAll();
	}

	public function getById($id) {
        $select = $this->_db
        	       ->select()
        	       ->from('product', null)
        	       ->join('product_status', 'product_status.id = product.product_status_id', null)
        	       ->where('product.status =? ', self::STATUS_ACTIVE)
        	       ->where('product.id =? ', $id)
        	       ->columns(array(
        	               'id'                => 'product.id',
        	               'code'              => 'product.code',
        	               'name'              => 'product.name',
        	               'color'             => 'product.color',
        	               'size'              => 'product.size',
        	               'quantity'          => 'product.quantity',
        	               'price_original'    => 'product.price_original',
        	               'price_sale'        => 'product.price_sale',
        	               'description'       => 'product.description',
        	               'thumbnail_link'    => 'product.thumbnail_link',
        	               'is_sale_off'       => 'product.is_sale_off',
        	               'product_category_id' => 'product.product_category_id',
        	               'meta_keyword'        => 'product.meta_keyword',
                           'meta_description'    => 'product.meta_description',
          				   'header_title'		 => 'product.header_title',
        	               'status_title'        => 'product_status.title'
        	       ));

        return $select->query()->fetch();
	}

}